﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using todo.Model;

namespace todo.Interop
{
    [ComVisible(true)]
    public class Interop
    {
        public Todo NewTodo()
        {
            return new Todo();
        }

        public Todos NewTodos()
        {
            return new Todos();
        }

        public void SetTodoTodos(Todo todo, Todos todos)
        {
            todo.Todos = todos.GetList();
        }

        public void SetTodoTags(Todo todo, string str)
        {
            var arr = str.Split(' ');
            if (arr.Length == 1 && str.Length == 0)
            {
                arr = new string[0];
            }
            var tags = new List<string>(arr);
            todo.Tags = tags;
        }

        public void SetTodoDisplayStyle(Todo todo, string str)
        {
            var displayStyle = (DisplayStyle) Enum.Parse(typeof (DisplayStyle), str, true);

            todo.DisplayStyle = displayStyle;
        }

        public Reminders NewReminders()
        {
            return new Reminders();
        }

        public Reminder NewReminder()
        {
            return new Reminder();
        }

        public void SetReminderType(Reminder reminder, string str)
        {
            var reminderType = (ReminderType) Enum.Parse(typeof (ReminderType), str, true);
            reminder.Type = reminderType;
        }

        public Timing NewTiming()
        {
            return new Timing();
        }

        public void SetTimingType(Timing timing, string str)
        {
            var timingType = (TimingType) Enum.Parse(typeof (TimingType), str, true);
            timing.Type = timingType;
        }

        public void SetTodoReminders(Todo todo, Reminders reminders)
        {
            todo.Reminders = reminders.GetList();
        }

        public void SetTodoCreateTime(Todo todo, string str)
        {
            var time = DateTime.MinValue;
            if (!string.IsNullOrEmpty(str))
            {
                time = DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
            }
            todo.CreateTime = time;
        }

        public void SetTodoCheckTime(Todo todo, string str)
        {
            var time = DateTime.MinValue;
            if (!string.IsNullOrEmpty(str))
            {
                time = DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
            }
            todo.CheckTime = time;
        }

        public static List<double> ParseDoubleList(string str, char sep)
        {
            var list = new List<double>();
            var parts = str.Split(sep);
            if (parts.Length == 1 && str.Length == 0)
            {
                return list;
            }
            foreach (var part in parts)
            {
                double value;
                value = double.Parse(part);
                list.Add(value);
            }
            return list;
        }

        public CollapsedItemInfo NewCollapsedItemInfo()
        {
            return new CollapsedItemInfo();
        }

        public CollapsedItemInfos NewCollapsedItemInfos()
        {
            return new CollapsedItemInfos();
        }

        public static string SerializeReminders(List<Reminder> reminders)
        {
            var sb = new StringBuilder();
            sb.Append('[');
            foreach (var reminder in reminders)
            {
                if (sb.Length > 1)
                {
                    sb.Append(',');
                }
                sb.Append('{')
                        .Append("\"Type\":\"")
                        .Append((int) reminder.Type)
                        .Append("\",")
                        .Append("\"Timing\":{")
                        .Append("\"Type\":\"").Append((int) reminder.Timing.Type)
                        .Append("\",")
                        .Append("\"Value\":\"").Append(reminder.Timing.Value).Append('"')
                        .Append('}')
                        .Append('}');
            }
            sb.Append(']');
            return sb.ToString();
        }
    }
}